//||=========================================================================||
//||                                                                         ||
//||                           CounterTemplate.cpp                           ||
//||                                                                         ||
//||                           by the_best_goose))                           ||
//||                                                                         ||
//||=========================================================================||

#include <string>
#include <assert.h>
#include <iostream>
#include <vector>
#include "../sources/FastComplex.h"
#include "../sources/FastComplex.cpp"

using std::string;
using std::cout;
using std::vector;

//-----------------------------------------------------------------------------

#define C c
#define Z z
#define T t

my_inline void Iterate(FastComplex& z, FastComplex& c, FastComplex& t);

//-----------------------------------------------------------------------------

extern "C" void MandelbrotCounter(FastComplex t, FastComplex* iter_points,
		                          int iter_points_count, const bool* pbreak,
		                          const int* index_array, int iters,
		                          int px_width, int px_height, int* pixels,
		                          double x, double y, double width,
		                          double height, int x_before, int x_after,
		                          int y_trans)
{
    assert(pixels);
    assert(index_array);
    assert(pbreak);
    int iters_count_max = 0;
    int iters_count = 0;
    int k = 0;
    int j = 0;

    FastComplex z;
    FastComplex c;

    for(int i = 0; i < px_width; ++i)
        for(j = 0; j < px_height; ++j)
        {
            iters_count_max = 0;
        	for(k = 0; k < iter_points_count; ++k)
        	{
                if(*pbreak)
                    return;
                z.re = iter_points[k].re;
                z.im = iter_points[k].im;
                c.re = (double)i * width / px_width + x;
                c.im = (double)j * height / px_height + y;
                for(iters_count = 0; iters_count < iters; ++iters_count)
                {
                    Iterate(z, c, t);
                    if(z.re * z.re + z.im * z.im > 1000)
                        break;
                }
                if(iters_count > iters_count_max)
                    iters_count_max = iters_count;
            }
            pixels[(j + y_trans) * (px_width + x_before + x_after) +
                   x_before + i] = index_array[iters_count_max];
        }
}

//-----------------------------------------------------------------------------
/*
extern "C" void JuliaCounter ( MandelbrotCounter* pcounter )
    {
    assert ( pcounter->pixels_ != NULL );
    assert ( pcounter->pwgt_   != NULL );
    MyComplex c, z;
    t = MyComplex ( pcounter->t_.x ( ), pcounter->t_.y ( ) );
    int iters_count = 0;
    int j = 0;
    for ( int i = 0; i < pcounter->px_size_.width ( ); ++i )
        for ( j = 0; j < pcounter->px_size_.height ( ); ++j )
            {
            if ( pcounter->break_ )
                return;
            c.re = pcounter->c_.x ( );
            c.im = pcounter->c_.y ( );
            z.re = i * pcounter->graph_rect_.width  ( ) / pcounter->px_size_.width  ( ) + pcounter->graph_rect_.x ( );
            z.im = ( j + pcounter->y_trans_ ) * pcounter->graph_rect_.height ( ) / pcounter->px_size_.height ( ) + pcounter->graph_rect_.y ( );
            for ( pcounter->iters_count_ = 0; pcounter->iters_count_ < pcounter->iters_; ++pcounter->iters_count_ )
                {
                Iterate ( z, c );
                if ( z.re * z.re + z.im * z.im > 100 )
                    break;
                }
            pcounter->pixels_ [ j * pcounter->px_size_.width ( ) + i ] = pcounter->index_array_ [ pcounter->iters_count_ ];
            }
    }
*/
//-----------------------------------------------------------------------------


